package net.asg.games.yokeltowers.view;

import net.asg.games.yokeltowers.images.ImageProvider;
import net.asg.games.yokeltowers.sounds.SoundManager;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.utils.Array;

public class ColumnYokelGameObject extends YokelGameObject {
	public Array<BlockYokelGameObject> column;

	public ColumnYokelGameObject(ImageProvider imageProvider,
			TextureRegion[] textureRegions, SoundManager soundManager,
			int width, int height, boolean isHitboxActive) {
		super(imageProvider, textureRegions, soundManager, width, height,
				isHitboxActive);

		column = new Array<BlockYokelGameObject>();
	}

	public void addBlock(BlockYokelGameObject block){
		column.add(block);
	}

	public BlockYokelGameObject getBlock(int blockNumber){
		return column.get(blockNumber);
	}

	public void draw(SpriteBatch batch, float delta) {
		for (BlockYokelGameObject block : column){
			block.draw(batch, delta);
		}
	}

	public void moveLeft(float delta) {
		rect.x -= rect.width;
		for (BlockYokelGameObject block : column){
			block.rect.x -= rect.width;
		}
	}

	public void moveDown(float delta, float speedBonus) {
		if (velocity < moveSpeed)
			velocity = velocity + (OBJECT_FRAMES * delta);
		rect.y -= velocity * delta * speedBonus;
		for (BlockYokelGameObject block : column){
			block.rect.y -= velocity * delta * speedBonus;
		}
	}
	
	public void moveRight(float delta) {
		rect.x += rect.width;
		for (BlockYokelGameObject block : column){
			block.rect.x += rect.width;
		}
	}

	public void setX(int x){
		rect.setX(x);
		for (BlockYokelGameObject block : column){
			block.rect.setX(x);
		}
	}

	public void setY(int y){
		rect.setY(y);
		int t = 0;
		for (BlockYokelGameObject block : column){
			block.rect.setY(y + (block.rect.height * t));
			t++;
		}
	}

	public void keepInBounds(Rectangle bounds) {
		if (rect.x <= bounds.x || column.get(0).rect.x <= bounds.x || column.get(1).rect.x <= bounds.x || 
				column.get(2).rect.x < bounds.x) {
			rect.setX(bounds.x);
			for (BlockYokelGameObject block : column){
				block.rect.setX(bounds.x);
			}
		}

		if (rect.x + width >= (bounds.width + bounds.x ) || column.get(0).rect.x >= (bounds.width + bounds.x ) || 
				column.get(1).rect.x >= (bounds.width + bounds.x ) || column.get(2).rect.x >= (bounds.width + bounds.x )) {
			rect.setX((bounds.width + bounds.x ) - width);
			for (BlockYokelGameObject block : column){
				block.rect.setX((bounds.width + bounds.x ) - width);
			}
		}
	}

	public boolean isBottomOfBounds(Rectangle bounds) {
		if(column.size > 0)
			return (column.peek().rect.y + column.peek().rect.height) < bounds.y;
		else
			return true;
	}
}
